#include "bsp.h"

unsigned char Address;

unsigned char Ser2_TX_Buf[40]={0x55,0xAA,0x55,0xAA};
unsigned char Ser2_RX_Buf[30]={0};
unsigned char Ser2_DA_Long,Ser2_Zda_Lg,Ser2_YXA_Lg,Ser2_TM_JS,Ser2_TM_Flag,Ser2_RX_Flag;

unsigned char Ser4_DA_Long,Ser4_Zda_Lg,Ser4_YXA_Lg,Ser4_TM_JS,Ser4_TM_Flag,Ser4_RX_Flag;
unsigned char Ser4_RX_Buf[30] = {0};
unsigned char Ser4_TX_Buf[40]={0x55,0xAA,0x55,0xAA};

// HAL库定时器更新中断回调函数
void TIM2_Callback(TIM_HandleTypeDef *htim)
{
    if (htim->Instance == TIM2) // 检查是否是TIM2触发的中断
    {
        // 串口2超时处理
        if(Ser2_TM_Flag == 1)
        {
            Ser2_TM_JS++;
            if(Ser2_TM_JS > 5)
            {
                // 串口接收同步头正确，第二BYTE接收以后超时判断
                Ser2_DA_Long = 0;    // 重新判断同步头                    
                Ser2_Zda_Lg = 0;
                Ser2_TM_JS = 0;      // 超时判断清除
                Ser2_TM_Flag = 0;
            }
        }
        

        // 串口4超时处理
        if(Ser4_TM_Flag == 1)
        {
            Ser4_TM_JS++;
            if(Ser4_TM_JS > 5)
            {
                // 串口接收同步头正确，第二BYTE接收以后超时判断
                Ser4_DA_Long = 0;    // 重新判断同步头                    
                Ser4_Zda_Lg = 0;
                Ser4_TM_JS = 0;      // 超时判断清除
                Ser4_TM_Flag = 0;
            }
        }
    }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	UART2_Callback(huart);
	UART4_Callback(huart);
}
void UART2_Callback(UART_HandleTypeDef *huart)
{
	if (huart->Instance == USART3)
	{
    chry_ringbuffer_write(&shell_rx_rb, &shell_rx_ch, 1);
    HAL_UART_Receive_IT(&huart3, (uint8_t *)&shell_rx_ch, 1);
	}
}
void UART4_Callback(UART_HandleTypeDef *huart)
{
	if (huart->Instance == UART4) // 检查是否是UART4触发的中断
    {
        uint8_t Recv = huart->Instance->DR; // 直接读取数据寄存器获取接收到的字节
        
        switch(Ser4_DA_Long)
        {
            case 0:
                if(Recv == 0x55)
                {
                    Ser4_DA_Long = 1;					
                    Ser4_Zda_Lg = 0;
                    Ser4_RX_Buf[Ser4_Zda_Lg] = Recv;
                    Ser4_Zda_Lg++;
                    // 启动定时器，此条命令延迟判断时间是否超时
                    Ser4_TM_JS = 0;
                    Ser4_TM_Flag = 1;	
                }
                else
                {
                    Ser4_DA_Long = 0; // 重新判断同步头					
                    Ser4_Zda_Lg = 0; 
                    Ser4_TM_Flag = 0;
                    Ser4_TM_JS = 0;
                }
                break;
                
            case 1:
                if((Ser4_RX_Buf[0] == 0x55) && (Recv == 0xAA))
                {
                    Ser4_RX_Buf[Ser4_Zda_Lg] = Recv;
                    Ser4_Zda_Lg++;
                    Ser4_DA_Long = 2;										 
                }
                else 
                {
                    Ser4_DA_Long = 0; // 重新判断同步头					
                    Ser4_Zda_Lg = 0; 
                    Ser4_TM_Flag = 0;
                    Ser4_TM_JS = 0;
                }
                break;
                
            case 2:
                if((Ser4_RX_Buf[0] == 0x55) && (Ser4_RX_Buf[1] == 0xAA) && (Recv == 0x55))
                {
                    Ser4_RX_Buf[Ser4_Zda_Lg] = Recv;
                    Ser4_Zda_Lg++;
                    Ser4_DA_Long = 3;										 
                }
                else 
                {
                    Ser4_DA_Long = 0; // 重新判断同步头					
                    Ser4_Zda_Lg = 0; 
                    Ser4_TM_Flag = 0;
                    Ser4_TM_JS = 0;
                }
                break;
                
            case 3:
                if((Ser4_RX_Buf[0] == 0x55) && (Ser4_RX_Buf[1] == 0xAA) && (Ser4_RX_Buf[2] == 0x55) && (Recv == 0xAA))
                {
                    Ser4_RX_Buf[Ser4_Zda_Lg] = Recv;
                    Ser4_Zda_Lg++;
                    Ser4_DA_Long = 4;										 
                }
                else 
                {
                    Ser4_DA_Long = 0; // 重新判断同步头					
                    Ser4_Zda_Lg = 0; 
                    Ser4_TM_Flag = 0;
                    Ser4_TM_JS = 0;
                }
                break;
                
            case 4:
                if((Recv == Address) || (Recv == 0xFF))
                {
                    Ser4_RX_Buf[Ser4_Zda_Lg] = Recv;
                    Ser4_Zda_Lg++;
                    Ser4_DA_Long = 5;	
                    // Ser4_YXA_Lg = Ser4_RX_Buf[4]-6; // 注释掉，后面再设置
                }										
                else 
                {
                    Ser4_DA_Long = 0; // 重新判断同步头					
                    Ser4_Zda_Lg = 0; 
                    Ser4_TM_Flag = 0;
                    Ser4_TM_JS = 0;
                }
                break;
                                         
            case 5:
                if((Ser4_RX_Buf[4] == Address) || (Ser4_RX_Buf[4] == 0xFF))
                {
                    Ser4_RX_Buf[Ser4_Zda_Lg] = Recv;
                    Ser4_Zda_Lg++;
                    Ser4_DA_Long = 6;	
                    Ser4_YXA_Lg = Recv - 6; // 根据接收到的字节设置剩余数据长度
                }										
                else 
                {
                    Ser4_DA_Long = 0; // 重新判断同步头					
                    Ser4_Zda_Lg = 0; 
                    Ser4_TM_Flag = 0;
                    Ser4_TM_JS = 0;
                }
                break;				 
                                         
            case 6:		
                Ser4_RX_Buf[Ser4_Zda_Lg] = Recv;
                Ser4_Zda_Lg++;
                Ser4_YXA_Lg--;						
                
                if(Ser4_YXA_Lg == 0) // =0数据接收完成
                {
//                    CRC16 = 0;
//                    CRC16H = 0;
//                    CRC16L = 0;
//                    
//                    // 计算CRC16校验（需要实现GetCRC16函数）
//                    CRC16 = GetCRC16(Ser4_RX_Buf, Ser4_RX_Buf[5] - 6);
//                    CRC16H = CRC16 >> 8;
//                    CRC16L = CRC16 & 0xFF;
                    
//                    // 验证CRC和数据尾
//                    if((Ser4_RX_Buf[0] == 0x55) && 
//                       (Ser4_RX_Buf[Ser4_RX_Buf[5] - 6] == CRC16L) && 
//                       (Ser4_RX_Buf[Ser4_RX_Buf[5] - 5] == CRC16H) && 
//                       (Ser4_RX_Buf[Ser4_RX_Buf[5] - 2] == 0x16) && 
//                       (Ser4_RX_Buf[Ser4_RX_Buf[5] - 1] == 0xE9))
//                    {
//                        // 根据命令执行相应操作
////                        if(Ser4_RX_Buf[6] == 0xB5)
////                        {
////                            B5_TanTouLedPower_ON();  // 开探头灯
////                        }
////                        else if(Ser4_RX_Buf[6] == 0xB6)
////                        {
////                            B6_TanTouLedPower_OFF();  // 关探头灯
////                        }
////                        else
////                        {
////                            Ser4_RX_Flag = 1;
////                        }
//                    }
                    
                    // 重置状态
                    Ser4_DA_Long = 0; // 重新判断同步头					
                    Ser4_Zda_Lg = 0; 
                    Ser4_TM_Flag = 0;
                    Ser4_TM_JS = 0;
                }					
                break;
                
            default:
                break;	 
        }
        
        // 重新启动接收中断
        HAL_UART_Receive_IT(&huart4, &Recv, 1);
    }
}
